<?php

class Tablet_IndexController extends Zend_Controller_Action
{

    public function init()
    {
        Zend_Session::start();
        $Session = new Zend_Session_Namespace ('Params');
        unset ($Session->Type);
        unset ($Session->Search);
        unset ($Session->Saved);
        $this->view->Persisted = $Session->Persisted;
        $this->_redirector = $this->_helper->getHelper('Redirector');   
        $this->_helper->layout->setLayout('ios');
        $this->view->addHelperPath(APPLICATION_PATH . '/modules/default/views/helpers/', 'SplashPage');
        $this->view->title        = "CloudNZB - Home"; 

    }

    public function indexAction()
    {
        $request    = $this->getRequest(); 
        $pagenumber = $request->getParam('page');  
        $pageparam  = $request->getParam('param');  
        if (!isset($pagenumber)) $pagenumber = 1;



        if (isset($pageparam)) {
            $pageparam   = strtolower($pageparam);
            $target      = "/request/findgroup/param/{$pageparam}";
            $destination = $this->view->url( array(  "action"=>"find", 'page'=>1, 'param'=>$pageparam   ) );
            $this->_helper->getHelper("FlashMessenger")->addMessage($destination);
            $this->_redirector
                 ->gotoUrl($target); 
        }


        $Profile = new Default_Model_UserProfile; 
        $this->view->groups       = $Profile->groups; 
        $this->view->links = array ();

        $bm = $this->view->url(array('controller'=>'favorites','action'=>'index'));
        $this->view->links = array ( 
                                     array ('text'=>'Home', 'active'=>true),
                                     array ('text'=>'Bookmarks', 'href'=>$bm),
                                     array ('text'=>'Media', 'href'=>'/tablet/media/')
                              );
        $data = $this->view->SplashPage($pagenumber, 10);
        $this->view->pageNumber   = $pagenumber; 
        $this->view->randomName   = $data["randomName"]; 
        $this->view->random       = $data["random"]; 
        $this->view->groups       = $data["groups"]; 
        $this->view->fullList     = $data["fullList"];   
    }

    public function findAction()
    {
        $bm = $this->view->url(array('controller'=>'favorites','action'=>'index'));
        $this->view->links = array ( 
                                     array ('text'=>'Home', 'href'=>'/tablet/index/index'),
                                     array ('text'=>'Bookmarks', 'href'=>$bm),
                                     array ('text'=>'Media', 'href'=>'/tablet/media/'),
                                     array ('text'=>'Find groups', 'active'=>true)
                              );

        $g = new Default_Model_NewsServer; 

        $request    = $this->getRequest(); 
        $pagenumber = $request->getParam('page');  
        $findParam  = $request->getParam('param');  
        if (!isset($pagenumber)) $pagenumber = 1;
        $startIndex = ($pagenumber - 1) * 10;

        if (isset($findParam)) {  
            $g->GetGroups($findParam);
            $sliced = array_slice ($g->groups, $startIndex, 10);
            $this->view->groups = $sliced; 
            $this->view->size   = sizeof($g->groups);  
            $this->view->param  = $findParam; 
            $this->view->page   = $pagenumber; 
        }








    }


}



